<?php

namespace app\staff\controller;

use app\common\controller\StaffBase;
use think\Db;

/**
 * 義工管理
 *
 * @icon fa fa-circle-o
 */
class Volunteerbuf extends StaffBase
{
    
    /**
     * Volunteer模型对象
     * @var \app\admin\model\Volunteer
     */
    protected $model = null;
    protected $user_model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Volunteer;
        $this->user_model = model('User');
        $this->view->assign("serviceTimeTypeList", $this->model->getServiceTimeTypeList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','volunteerspecialty','staff'])
                    ->where($where)
                    ->where(['volunteer.staff_id'=>$this->auth->id])
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['nickname','avatar','status']);
				$row->getRelation('volunteerspecialty')->visible(['name']);
                $row->getRelation('staff')->visible(['nickname']);
                $region_address = db('region')->where(['id'=>$row->province])->value('name');
                $city = db('region')->where(['id'=>$row->city])->value('name');
                if ( $city ) {
                    $region_address = $region_address.$city;
                    $district = db('region')->where(['id'=>$row->district])->value('name');
                    if ( $district ) {
                        $region_address = $region_address.$district;
                    }
                }
                $row->region_address = $region_address?$region_address:'';
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig("updateuser", $this->auth->check("volunteerbuf/updateuser"));
        $this->assignconfig("caring_task", $this->auth->check("caring_task_order/task_list"));
        $this->assignconfig("training_tasks", $this->auth->check("training_task_order/task_list"));
        return $this->view->fetch();
    }

    /**
     * 启用合作
     */
    public function updateuser($ids = "")
    {
        if ($ids) {
            $ids = db('volunteer')->where(['id'=>['in',$ids]])->column('user_id');
            $pk = $this->user_model->getPk();
            $list = $this->user_model->where($pk, 'in', $ids)->select();
            $title = '';
            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    if ($v['status'] == 'hidden') {
                        $where = ['status'=>'normal'];
                    }else{
                        $where = ['status'=>'hidden'];;
                    }
                    $count += $v->save($where);
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('操作失败'));
            }
        }
        $this->error(__('操作失败', 'ids'));
    }

}
